//
//  BeautySalonUserTC.swift
//  甜言蜜语
//
//  Created by yingli_yanfa_liuxingyu on 16/8/10.
//  Copyright © 2016年 wl. All rights reserved.
//

import UIKit
import ActionSheetPicker_3_0
import SVProgressHUD
class BeautySalonUserTC: UITableViewController,UICollectionViewDataSource, UICollectionViewDelegate, MTImagePickerControllerDelegate, ActionSheetCustomPickerDelegate,FactoryCollCellDelegate  {
    
    private var dataSource = [MTImagePickerModel]()//照片的数据源
    var modelList: loginInfo?{
        didSet{
            
        }
    }
   //
    var msg: regiestInfo?
    var memberType: String!
    var type: String!
    var theMainArr: [String] = []//主营
    var collection_Style_Use: Bool!//是否选了职业风采
    var collection_License_Use: Bool!//是否选择了营业执照
    var licenseCount: Int = 0//执照照片的数量
    var styleCount: Int = 0//风采照片的数量
    
    var collectionStyleArr: [UIImage] = []//风采照片的流文件
    var collectionLicenseArr: [UIImage] = []//执照照片的流文件
    var img: UIImage?
    var typeStr: String!
    
    @IBOutlet weak var collection_Style: UICollectionView!//风采
    @IBOutlet weak var collection_License:UICollectionView!//执照
    @IBOutlet weak var BusinessLicense: UITextField!
    @IBOutlet weak var workArea: UILabel!
    @IBOutlet weak var workAreaPlace: UILabel!
    @IBOutlet weak var address: UITextField!
    @IBOutlet weak var personnum: UILabel!
    @IBOutlet weak var personnumPlace: UILabel!
    @IBOutlet weak var storearea: UILabel!
    @IBOutlet weak var storeareaPlace: UILabel!
    @IBOutlet weak var studioBtn: UIButton!
    @IBOutlet weak var entityShopBtn: UIButton!
    @IBOutlet weak var mr: UIButton!
    @IBOutlet weak var mtjf: UIButton!
    @IBOutlet weak var wx: UIButton!
    @IBOutlet weak var SPABtn: UIButton!
    @IBOutlet weak var jkys: UIButton!
    @IBOutlet weak var yxzx: UIButton!
    @IBOutlet weak var mz: UIButton!
    
    
    @IBOutlet weak var photoImg: UIButton!
    @IBOutlet weak var photoImg2: UIButton!
    
    var deleteBtnFlagStyle:Bool!
    var rotateAniFlagStyle:Bool!
    
    var deleteBtnFlagLicense:Bool!
    var rotateAniFlagLicense:Bool!
   
    var index1: NSInteger = 0 // 省下标
    var index2: NSInteger = 0 // 市下标
    var index3: NSInteger = 0 // 区下标
    
    var provinceArr: NSArray! //省
    var countryArr: NSArray! // 市
    var districtArr: NSArray! // 区
    var addressArr: NSArray! // 解析出来的最外层的数组
    
    var photoTypeLicense: Bool!//照片的type 营业执照
    var photoTypeStyle: Bool!// 企业风采
    
    override func viewDidLoad() {
        super.viewDidLoad()
        publicMethod.setHUDStyle()
        self.photoTypeLicense = false
        self.photoTypeStyle = false
        self.mr.layer.masksToBounds = true
        self.mr.layer.borderWidth = 1
        self.mr.layer.borderColor = BODER_COLOR.CGColor
        
        self.mtjf.layer.masksToBounds = true
        self.mtjf.layer.borderWidth = 1
        self.mtjf.layer.borderColor = BODER_COLOR.CGColor
        
        self.wx.layer.masksToBounds = true
        self.wx.layer.borderWidth = 1
        self.wx.layer.borderColor = BODER_COLOR.CGColor
        
        self.SPABtn.layer.masksToBounds = true
        self.SPABtn.layer.borderWidth = 1
        self.SPABtn.layer.borderColor = BODER_COLOR.CGColor
        
        self.jkys.layer.masksToBounds = true
        self.jkys.layer.borderWidth = 1
        self.jkys.layer.borderColor = BODER_COLOR.CGColor
        
        self.yxzx.layer.masksToBounds = true
        self.yxzx.layer.borderWidth = 1
        self.yxzx.layer.borderColor = BODER_COLOR.CGColor
        
        self.mz.layer.masksToBounds = true
        self.mz.layer.borderWidth = 1
        self.mz.layer.borderColor = BODER_COLOR.CGColor
        
       
        
        self.collection_License.hidden = true
        self.collection_License.scrollEnabled = false
        self.collection_License_Use = false
        self.collection_Style.hidden = true
        self.collection_Style.scrollEnabled = false
        self.collection_Style_Use = false
        
        self.studioBtn.setImage(UIImage(named: "fangkuang"), forState: UIControlState.Normal)
        
        self.entityShopBtn.setImage(UIImage(named: "fangkuang"), forState: UIControlState.Normal)
        
        let cellNib = UINib(nibName: "FactoryCollCell", bundle: nil)
        self.collection_License.registerNib(cellNib, forCellWithReuseIdentifier: "FactoryCollCell")
        
        let cellNib1 = UINib(nibName: "FactoryCollCell", bundle: nil)
        self.collection_Style.registerNib(cellNib1, forCellWithReuseIdentifier: "FactoryCollCell")
        
        self.title = "信息补全"
        self.img = UIImage(named: "placeholder")
        deleteBtnFlagStyle = true
        rotateAniFlagStyle = true
        
        deleteBtnFlagLicense = true
        rotateAniFlagLicense = true
        self.addDoubleTapGesture()
        
        self.calculateFirstData()
    
    }

    override func viewWillDisappear(animated: Bool) {
        //SVProgressHUD.dismiss()
        super.viewWillDisappear(animated)
    }
    func addDoubleTapGesture() {
        let doubletapStyle = UITapGestureRecognizer(target: self, action: #selector(self.handleDoubleTapStyle(_:)))
        doubletapStyle.numberOfTapsRequired = 2
        
        let doubletapLicense = UITapGestureRecognizer(target: self, action: #selector(self.handleDoubleTapLicense(_:)))
        doubletapLicense.numberOfTapsRequired = 2
        
        self.collection_License!.addGestureRecognizer(doubletapLicense)
        self.collection_Style.addGestureRecognizer(doubletapStyle)
        
    }
    
    func handleDoubleTapStyle(gestureRecognizer: UITapGestureRecognizer) {
        self.hideAllDeleteBtnStyle()
    }
    
    func handleDoubleTapLicense(gestureRecognizer: UITapGestureRecognizer) {
        self.hideAllDeleteBtnLicense()
    }
    
    func hideAllDeleteBtnLicense(){
        if !deleteBtnFlagLicense {
            deleteBtnFlagLicense = true
            rotateAniFlagLicense = true
            self.collection_License.reloadData()
        }
    }
    
    func hideAllDeleteBtnStyle() {
        if !deleteBtnFlagStyle {
            deleteBtnFlagStyle = true
            rotateAniFlagStyle = true
            self.collection_Style.reloadData()
        }
        
    }

     //主营按钮点击事件
    @IBAction func theMainClicked(sender: UIButton){
        
        if sender == self.mr{
            sender.selected = !sender.selected
            if sender.selected{
                self.mr.backgroundColor = NAVIGATIONBAR_RED_COLOR
                
                self.theMainArr.append((self.mr.titleLabel?.text)!)
            }else{
                self.mr.backgroundColor = UIColor.whiteColor()
                self.theMainArr.removeAtIndex(self.theMainArr.indexOf((self.mr.titleLabel?.text)!)!)
            }
            
        }
        if sender == self.mtjf{
            sender.selected = !sender.selected
            if sender.selected{
                self.mtjf.backgroundColor = NAVIGATIONBAR_RED_COLOR
                self.theMainArr.append((self.mtjf.titleLabel?.text)!)
            }else{
                self.mtjf.backgroundColor = UIColor.whiteColor()
                self.theMainArr.removeAtIndex(self.theMainArr.indexOf((self.mtjf.titleLabel?.text)!)!)
            }
        }
        if sender == self.wx{
            sender.selected = !sender.selected
            if sender.selected{
                self.wx.backgroundColor = NAVIGATIONBAR_RED_COLOR
                self.theMainArr.append((self.wx.titleLabel?.text)!)
            }else{
                self.wx.backgroundColor = UIColor.whiteColor()
                self.theMainArr.removeAtIndex(self.theMainArr.indexOf((self.wx.titleLabel?.text)!)!)
            }
        }
        if sender == self.SPABtn{
            sender.selected = !sender.selected
            if sender.selected{
                self.SPABtn.backgroundColor = NAVIGATIONBAR_RED_COLOR
                self.theMainArr.append((self.SPABtn.titleLabel?.text)!)
 
            }else{
                self.SPABtn.backgroundColor = UIColor.whiteColor()
                self.theMainArr.removeAtIndex(self.theMainArr.indexOf((self.SPABtn.titleLabel?.text)!)!)
            }
        }
        if sender == self.jkys{
            sender.selected = !sender.selected
            if sender.selected{
                self.jkys.backgroundColor = NAVIGATIONBAR_RED_COLOR
                self.theMainArr.append((self.jkys.titleLabel?.text)!)
            }else{
                self.jkys.backgroundColor = UIColor.whiteColor()
                self.theMainArr.removeAtIndex(self.theMainArr.indexOf((self.jkys.titleLabel?.text)!)!)
            }
            
        }
        if sender == self.yxzx{
            sender.selected = !sender.selected
            if sender.selected{
                self.yxzx.backgroundColor = NAVIGATIONBAR_RED_COLOR
                self.theMainArr.append((self.yxzx.titleLabel?.text)!)
            }else{
                self.yxzx.backgroundColor = UIColor.whiteColor()
                self.theMainArr.removeAtIndex(self.theMainArr.indexOf((self.yxzx.titleLabel?.text)!)!)
            }
            
        }
        if sender == self.mz{
            sender.selected = !sender.selected
            if sender.selected{
                self.mz.backgroundColor = NAVIGATIONBAR_RED_COLOR
                self.theMainArr.append((self.mz.titleLabel?.text)!)
            }else{
                self.mz.backgroundColor = UIColor.whiteColor()
                self.theMainArr.removeAtIndex(self.theMainArr.indexOf((self.mz.titleLabel?.text)!)!)
            }
            
        }
        
    }
    
    
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if(cell.respondsToSelector(Selector("setSeparatorInset:"))) {
            cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0)
        }
        if(cell.respondsToSelector(Selector("setLayoutMargins:"))) {
            cell.layoutMargins = UIEdgeInsetsMake(0, 0, 0, 0)
        }
    }
    

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        if indexPath.row == 1{
            self.BusinessLicense.resignFirstResponder()
            self.address.resignFirstResponder()
            
            ActionSheetCustomPicker.showPickerWithTitle("位置区域", delegate: self, showCancelButton: true, origin: self.view, initialSelections: [index1, index2])
           
            
        }
        if indexPath.row == 3{
            self.BusinessLicense.resignFirstResponder()
            self.address.resignFirstResponder()
            ActionSheetMultipleStringPicker.showPickerWithTitle("员工人数", rows:[["10-20", "20-30", "30-50","100以上"]],initialSelection: [1,1], doneBlock: {
                picker, values, indexes in
                let obj = (indexes as! NSArray)[0] as! String
                self.personnumPlace.alpha = 0
                self.personnum.text = obj
                return
                }, cancelBlock: { ActionMultipleStringCancelBlock in return }, origin: self.view)
        }
        if indexPath.row == 4 {
            self.BusinessLicense.resignFirstResponder()
            self.address.resignFirstResponder()
            ActionSheetMultipleStringPicker.showPickerWithTitle("营业面积", rows:[["20-60平米", "60-100平米", "100-200平米","200平米以上"]],initialSelection: [1,1], doneBlock: {
                picker, values, indexes in
                let obj = (indexes as! NSArray)[0] as! String
                self.storeareaPlace.alpha = 0
                self.storearea.text = obj
                return
                }, cancelBlock: { ActionMultipleStringCancelBlock in return }, origin: self.view)
        }
        if indexPath.row == 7{
                    }
        if indexPath.row == 8{
            

        }

        
    }
    
    func calculateFirstData(){
        
        loadFirstData()
        let cityNameArr = NSMutableArray()
        
        print(addressArr.count)
        for cityName in (addressArr[index1].allValues.first) as! NSArray {
            
            let name1 = (cityName as! NSDictionary).allKeys.first
            cityNameArr.addObject(name1!)
        }
        // 组装对应省下面的市
        countryArr = cityNameArr
        
        districtArr = addressArr[index1].allObjects[0][index2].allValues[0] as! NSArray
    }
    func loadFirstData(){
        
        let path = NSBundle.mainBundle().pathForResource("address", ofType: "json")
        print(path)
        
        let data = NSData.init(contentsOfFile: path!)
        
        guard let myArr = try? NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) else {
            
            return
        }
        
        addressArr = myArr as! NSArray
        
        let firstName = NSMutableArray()
        for dict in addressArr {
            
            let name = (dict as! NSDictionary).allKeys.first
            firstName.addObject(name!)
        }
        provinceArr = firstName
        
        
    }
    //MARK:target
    //营业执照
    @IBAction func photoImgBtnSelected(sender: UIButton){
        self.collection_License_Use = true
        self.collection_Style_Use = false
        self.photoTypeLicense = true//营业执照
        self.photoTypeStyle = false
        self.btnPickTouch()
    }
    //企业风采
    @IBAction func photoImgBtn2Selected(sender: UIButton){
        self.collection_Style_Use = true
        self.collection_License_Use = false
        self.photoTypeStyle = false//企业风采
        self.photoTypeLicense = false
        self.btnPickTouch()
    }

    //MARK: UIPickerViewDataSource Implementation
    
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 3
    }
    func pickerView(_pickerView: UIPickerView,
                    numberOfRowsInComponent component: Int) -> Int{
        switch (component)
        {
        case 0: return self.provinceArr.count;
        case 1: return self.countryArr.count;
        default:break;
        }
        return 0;
    }
    
    func pickerView(pickerView: UIPickerView,
                    titleForRow row: Int,
                                forComponent component: Int) -> String?{
        
        switch (component)
        {
        case 0: return self.provinceArr[row] as? String
        case 1: return self.countryArr[row] as? String
        default:break;
        }
        return nil;
    }
    func pickerView(pickerView: UIPickerView,
                    viewForRow row: Int,
                               forComponent component: Int,
                                            reusing view: UIView?) -> UIView{
        
        
        let label: UILabel! = UILabel()
        label!.font = UIFont.systemFontOfSize(14)
        
        var title = ""
        switch component {
        case 0:
            title = self.provinceArr[row] as! String
        case 1:
            title = self.countryArr[row] as! String
        default:
            break
        }
        
        label!.textAlignment = NSTextAlignment.Center
        label!.text = title
        return label!
        
    }
    func pickerView(pickerView: UIPickerView,
                    didSelectRow row: Int,
                                 inComponent component: Int){
        switch component {
        case 0:
            self.index1 = row
            self.index2 = 0
            // 滚动的时候都要进行一次数组的刷新
            self.calculateFirstData()
            pickerView.reloadComponent(1)
            pickerView.selectRow(0, inComponent: 1, animated: true)
        case 1:
            self.index2 = row
        default:
            break
        }
        
    }
    //MARK:FactoryCollCellDelegate
    func deleteCellAtIndexpath(indexPath: NSIndexPath, cellView: UIView){
        if self.collection_License_Use == true{
            //self.dataSource.removeAtIndex(indexPath.row)
            self.collectionLicenseArr.removeAtIndex(indexPath.row)
            if self.collectionLicenseArr.count == 0{
                self.photoImg.hidden = false
                deleteBtnFlagLicense = true
                rotateAniFlagLicense = true
                //self.collection_License_Use = false
            }
            self.collection_License.reloadData()
            
        }else{
            // self.dataSource.removeAtIndex(indexPath.row)
            self.collectionStyleArr.removeAtIndex(indexPath.row)
            if self.collectionStyleArr.count == 0{
                self.photoImg2.hidden = false
                deleteBtnFlagStyle = true
                rotateAniFlagStyle = true
                //self.collection_Style_Use = false
            }
            
            self.collection_Style.reloadData()
            
        }
        
    }
    func showAllDeleteBtn(type: String){
        print("showAll")
        if type == "1"{
            deleteBtnFlagLicense = false
            rotateAniFlagLicense = false
            self.collection_License_Use = true
            self.collection_Style_Use = false
            self.collection_License.reloadData()
        }else{
            deleteBtnFlagStyle = false
            rotateAniFlagStyle = false
            self.collection_Style_Use = true
            self.collection_License_Use = false
            self.collection_Style.reloadData()
        }
        
    }
    //MARK:点击完成的按钮时执行
    func actionSheetPickerDidSucceed(actionSheetPicker: AbstractActionSheetPicker!, origin: AnyObject!) {
        var detailAddress = String()
        if self.index1 < self.provinceArr.count {
            let firstAddress = self.provinceArr[self.index1]
            detailAddress += firstAddress as! String
        }
        if self.index2 < self.countryArr.count {
            let secondAddress = self.countryArr[self.index2]
            detailAddress += secondAddress as! String
        }
        
        // 此界面显示
        print(detailAddress)
        self.workArea.text = detailAddress
        self.workAreaPlace.alpha = 0
        
    }
    
    // 类型按钮的点击事件
    @IBAction func theTypeBtnClicked(sender: UIButton){
        if sender == self.studioBtn{
            self.type = "工作室"
            self.studioBtn.setImage(UIImage(named: "fangkuang_selected"), forState: UIControlState.Normal)
            
            self.entityShopBtn.setImage(UIImage(named: "fangkuang"), forState: UIControlState.Normal)
            
        }
        if sender == self.entityShopBtn{
            self.type = "实体店铺"
            
            self.studioBtn.setImage(UIImage(named: "fangkuang"), forState: UIControlState.Normal)
            
            self.entityShopBtn.setImage(UIImage(named: "fangkuang_selected"), forState: UIControlState.Normal)
            
        }
    }
    //提交按钮的点击事件
    @IBAction func submitBtnClicked(sender: UIButton){
        if self.BusinessLicense.text == ""{
            SVProgressHUD.showErrorWithStatus("请填写美容院名称")
            
            return
        }
        if self.workArea.text == ""{
             SVProgressHUD.showErrorWithStatus("请选择位置区域")
            
            return
        }
        if self.address.text == ""{
            SVProgressHUD.showErrorWithStatus("请输入详细地址")
            
            return
        }
        if self.personnum.text == ""{
            SVProgressHUD.showErrorWithStatus("请选择员工人数")
            
            return
        }
        if self.storearea.text == ""{
            SVProgressHUD.showErrorWithStatus("请选择营业面积")
           
            return
        }
        if self.type == ""{
            SVProgressHUD.showErrorWithStatus("请选择类型")
           
            return
        }
        if self.type == ""{
            SVProgressHUD.showErrorWithStatus("请选择类型")
            
            return
        }
        if self.theMainArr.count == 0 {
            SVProgressHUD.showErrorWithStatus("请选择主营")
           
            return
        }else{
            self.compleInfo()
        }
    }
    
    
    //选取照片相关
    @available(iOS 8.0, *)
    func imagePickerController(picker: MTImagePickerController, didFinishPickingWithPhotosModels models: [MTImagePickerPhotosModel]) {
        self.dataSource = models
        if self.collection_License_Use == true{
            for index in 0...self.dataSource.count - 1{
                let model = self.dataSource[index]
                collectionLicenseArr.append(model.getThumbImage()!)
            }
            self.dataSource.removeAll()
            self.collection_License.hidden = false
            self.collection_License.dataSource = self
            self.collection_License.delegate = self
            self.collection_License.reloadData()
            self.photoImg.hidden = true
            
        }else{
            for index in 0...self.dataSource.count - 1{
                let model = self.dataSource[index]
                collectionStyleArr.append(model.getThumbImage()!)
            }
            self.dataSource.removeAll()
            self.collection_Style.hidden = false
            self.collection_Style.dataSource = self
            self.collection_Style.delegate = self
            self.photoImg2.hidden = true
            self.collection_Style.reloadData()
            
        }
    }
    
    func imagePickerController(picker: MTImagePickerController, didFinishPickingWithAssetsModels models: [MTImagePickerAssetsModel]) {
        self.dataSource = models
        if self.collection_License_Use == true{
            for index in 0...self.dataSource.count - 1{
                let model = self.dataSource[index]
                collectionLicenseArr.append(model.getThumbImage()!)
            }
            self.dataSource.removeAll()
            self.collection_License.hidden = false
            self.collection_License.dataSource = self
            self.collection_License.delegate = self
            self.collection_License.reloadData()
            self.photoImg.hidden = true
            
        }else{
            for index in 0...self.dataSource.count - 1{
                let model = self.dataSource[index]
                collectionStyleArr.append(model.getThumbImage()!)
            }
            self.dataSource.removeAll()
            self.collection_Style.hidden = false
            self.collection_Style.dataSource = self
            self.collection_Style.delegate = self
            self.photoImg2.hidden = true
            self.collection_Style.reloadData()
            
        }
        
        
    }
    
    
    
    func btnPickTouch() {
        
        var mediaTypes = [MTImagePickerMediaType]()
        let source = MTImagePickerSource.Photos
        mediaTypes.append(MTImagePickerMediaType.Photo)
        let vc = MTImagePickerController.instance
        vc.mediaTypes = mediaTypes
        vc.source = source
        vc.delegate = self
        vc.maxCount = 6
        let nc = UINavigationController(rootViewController: vc)
        self.presentViewController(nc, animated: true, completion: nil)

    }
    
    
    //MARK:UICollectionViewDataSource
    //每一组有多少个cell
    func collectionView(collectionView:UICollectionView,numberOfItemsInSection section: Int) -> Int{
        
        if self.collection_License_Use == true{
            if self.collectionLicenseArr.count == 0{
                return 0
            }else{
                return self.collectionLicenseArr.count
            }
        }else{
            if self.collectionStyleArr.count == 0{
                return 0
            }else{
                return self.collectionStyleArr.count
            }
        }

        
        
    }
    //设置每组的cell的边界
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,insetForSectionAtIndex section: Int) -> UIEdgeInsets{
        
        return UIEdgeInsetsMake(10, 10, 10, 10)//上左下右
    }
    //最小行间距
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat{
        return 10
    }
    //最小列间距
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat{
        return 10
    }
    //设定全局的Cell尺寸
    func collectionView(collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{
        
        return CGSizeMake(SCREEN_WIDTH/3-20,50)
        
    }
    //cell上显示的信息
    func collectionView(collectionView: UICollectionView,cellForItemAtIndexPath indexPath: NSIndexPath) ->UICollectionViewCell{
        
        if self.collection_License_Use == true{
            
            licenseCount += 1
            let cell = collection_License.dequeueReusableCellWithReuseIdentifier("FactoryCollCell", forIndexPath: indexPath) as! FactoryCollCell
            cell.delegate = self
            cell.photoType = "1"
            cell.indexPath = indexPath
            let model = self.collectionLicenseArr[indexPath.row]
            if deleteBtnFlagLicense == true{
                cell.deleteBtn.hidden = true
            }else{
                cell.deleteBtn.hidden = false
            }
            if (!rotateAniFlagLicense) {
                YTAnimation.vibrateAnimation(cell)
            }else{
                cell.layer.removeAnimationForKey("shake")
            }
            
            cell.iv.image = model
            
            return cell
            
        }else{
            
            let cell = collection_Style.dequeueReusableCellWithReuseIdentifier("FactoryCollCell", forIndexPath: indexPath) as! FactoryCollCell
            cell.delegate = self
            cell.photoType = "2"
            cell.indexPath = indexPath
            styleCount += 1
            let model = self.collectionStyleArr[indexPath.row]
            
            if deleteBtnFlagStyle == true{
                cell.deleteBtn.hidden = true
            }else{
                cell.deleteBtn.hidden = false
            }
            if (!rotateAniFlagStyle) {
                YTAnimation.vibrateAnimation(cell)
            }else{
                cell.layer.removeAnimationForKey("shake")
            }
            
            cell.iv.image = model
            
            return cell
        }
        
    }
    //MARK:NETWORKING
    //上传信息
    func compleInfo(){

        var param = ["membertypecn": self.memberType!, "name": self.BusinessLicense.text!, "unitarea": self.workArea.text!, "unitaddress": self.address.text!, "unitscale": self.personnum.text!, "unittype": self.type!]
        
        var unitMain: String = ""
        for index in 0...self.theMainArr.count - 1{
            
            var unitmainStr: String!
            
            if index == self.theMainArr.count - 1{
                unitmainStr = self.theMainArr[index] 
                
            }else{
                unitmainStr = ((self.theMainArr[index] ) + ",")
            }
            
            unitMain += unitmainStr
            
        }
        param["unitmain"] = unitMain
        
        DataTool.uploadLicense(self.collectionLicenseArr, uploadStyleImage: self.collectionLicenseArr, imgsrc: self.img!, type: "注册", param: param, completionHandler: { (newModel) in
                        guard let newDataes = newModel else {
                            return
                        }
            print(newDataes)
            self.msg = newDataes
            //分为三种个人,美容院,企业
        NSUserDefaults.standardUserDefaults().setObject(self.msg!.type, forKey: "k_UD_type")
            
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "k_UD_isComplete")
            
            self.loginAction()
            
            NSUserDefaults.standardUserDefaults().synchronize()
            
            SVProgressHUD.showSuccessWithStatus(self.msg?.msg)
                      
            self.navigationController?.popToRootViewControllerAnimated(true)
        })
    }
    func loginAction(){
        let token = NSUserDefaults.standardUserDefaults().stringForKey("k_UD_token")
        let phone = NSUserDefaults.standardUserDefaults().objectForKey("k_UD_user")
        let passWord = NSUserDefaults.standardUserDefaults().objectForKey("k_UD_pass")
        
        DataTool.login(phone! as! String, pass: passWord! as! String ,token: token, completionHandler:{ (newModel) in
            guard let newDataes = newModel else {
                return
            }
            print(newDataes)
            
            if newDataes.msg == "登陆成功"{
                
                NSUserDefaults.standardUserDefaults().setBool(true, forKey: "k_UD_isLogin")
                
                NSUserDefaults.standardUserDefaults().synchronize()
                
            }else{
               
                
                return
            }
            
        })
    }
    


   
}
